<?php
namespace app\common\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Console;

/**
 * 定时同步进行中的历史订单
 * @author King
 *
 */
class CronSyncHistoryOrder extends Command
{
	protected $logName = 'cronSyncHistoryOrder';

	protected function configure()
	{
		$this->setName('cronSyncHistoryOrder')
		->addOption('type', '1', Option::VALUE_OPTIONAL, '日期范围类型（1=10天内，2=10-20天，3=20天以上）', 1)
		->setDescription('定时同步进行中的历史订单');
	}

	protected function execute(Input $input, Output $output)
	{
		$type = $input->getOption('type');

		$msg = "历史订单同步type={$type}";
		consoleLog($msg);
		addLog($msg, $this->logName);

		$redis = getRedis();
		$orders = $redis->hGetAll(ORDER_ING);
		if (empty($orders)) {
			$orders = db('order')->where('status', 'in', [0, 1])->column('create_time', 'client_ordernum');
			if ($orders) {
				$redis->mset(ORDER_ING, $orders);
				$redis->expire(2*86400);
			}
		}

		if (empty($orders)) {
			$msg = "未发现在途订单";
			consoleLog($msg);
			addLog($msg, $this->logName);
			return ;
		}

		$timeList = [];
		$timeAgo = [1 => mktime(0,0,0,date('m'),date('d') - 10, date('Y')), 2 => mktime(0,0,0,date('m'),date('d') - 20, date('Y')), 3 => mktime(0,0,0,date('m'),date('d') - 20, date('Y'))];
		foreach ($orders as $createTime) {
			if ($type == 3 && $createTime < mktime(0,0,0,date('m'), date('d') - 20, date('Y'))) {
				$timeList[date('Ymd', $createTime)][] = $createTime;
			} elseif ($type == 2 && $createTime < mktime(0,0,0,date('m'), date('d') - 10, date('Y'))) {
				$timeList[date('Ymd', $createTime)][] = $createTime;
			} elseif ($type == 1 && $createTime < mktime(0,0,0,date('m'), date('d'), date('Y'))) {
				$timeList[date('Ymd', $createTime)][] = $createTime;
			}
		}

		foreach ($timeList as $timeArr) {
			$timeMin = min($timeArr);
			$timeMax = max($timeArr);

			if (date("d", $timeMin) == date("d", $timeMin-300)) {$timeMin = $timeMin - 300;}
			else $timeMin = mktime(0,0,0,date('m', $timeMin),date('d', $timeMin),date('Y', $timeMin));
			if (date("d", $timeMax) == date("d", $timeMax+300)) {$timeMax = $timeMax + 300;}
			else $timeMax = mktime(23,59,59,date('m', $timeMin),date('d', $timeMin),date('Y', $timeMin));

			$msg = "发起同步订单请求[".date('Y-m-d H:i:s', $timeMin)."] ~ [".date('Y-m-d H:i:s', $timeMax)."]";
			consoleLog($msg);
			addLog($msg, $this->logName);

			$isAutoImportArr = db('order')->where('client_ordernum', 'in', array_keys($orders))->where('create_time', 'between', [$timeMin, $timeMax])->distinct(true)->field('is_auto_import')->column('is_auto_import');
			foreach ($isAutoImportArr as $mes) {
				if (empty($mes)) continue;
				$params = ["--start={$timeMin}", "--end={$timeMax}", '--history=1', "--mes={$mes}"];
				$output = Console::call('cronSyncOrder', $params);
				$log = $output->fetch();
				$msg = "请求结果：" . json_encode($log, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
				consoleLog($msg);
				addLog($msg, $this->logName);
			}
		}

		$msg = "历史订单同步type={$type}完成";
		consoleLog($msg);
		addLog($msg, $this->logName);

		return;
	}
}